package com.yuexinduo.mainmodule;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.yuexinduo.mylibrary.base.BaseMvpActivity;
import com.yuexinduo.mylibrary.constants.PathConstants;

import java.util.ArrayList;
import java.util.List;


@Route(path = PathConstants.MAIN_ACTIVITY_PATH)
public class MainActivity extends BaseMvpActivity {
    private ViewPager mViewPager;
    private LinearLayout linearBtn;
    private TextView home;
    private TextView fine;
    private TextView mine;
    private List<Fragment> list_fragment = new ArrayList<>();
    private CustomViewPagerAdapter mCustomViewPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected Activity getActivity() {
        return this;
    }

    @Override
    protected void initCreate() {
        initFragment();


        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        linearBtn = (LinearLayout) findViewById(R.id.linear_btn);
        home = (TextView) findViewById(R.id.home);
        fine = (TextView) findViewById(R.id.fine);
        mine = (TextView) findViewById(R.id.mine);


        mCustomViewPagerAdapter = new CustomViewPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mCustomViewPagerAdapter);
        setTabClick();
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTabState(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    /**
     * 初始化获取4个片段
     */
    private void initFragment() {
        Fragment homeFragment = (Fragment) ARouter.getInstance().build(PathConstants.HOME_PATH).navigation();
        Fragment findFragment = (Fragment) ARouter.getInstance().build(PathConstants.FIND_HOME_PATH).navigation();
        Fragment mineFragment = (Fragment) ARouter.getInstance().build(PathConstants.MINE_HOME_PATH).navigation();
        list_fragment.add(homeFragment);
        list_fragment.add(findFragment);
        list_fragment.add(mineFragment);
    }
    private void setTabClick() {
        for (int position = 0;position < linearBtn.getChildCount();position++){
            TextView tv_tab = (TextView) linearBtn.getChildAt(position);
            final int finalPosition = position;
            tv_tab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mViewPager.setCurrentItem(finalPosition);
                }
            });
        }
    }

    private void setTabState(int selectPosition){
        for (int position = 0;position < linearBtn.getChildCount();position++){
            TextView tv_tab = (TextView) linearBtn.getChildAt(position);
            tv_tab.setSelected(selectPosition == position);
        }
    }

    class CustomViewPagerAdapter extends FragmentPagerAdapter {

        public CustomViewPagerAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list_fragment.get(position);
        }

        @Override
        public int getCount() {
            return list_fragment.size();
        }
    }


}